package com.digitalxpert.jargle.TestGui;
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
public class TextComponentPopupMenu extends JPopupMenu {
	private Action copyA;
	private Action pasteA;
	private Action selectAllA;
	private Action cutA;
	private JTextComponent textComp;
	private Boolean canModify;
	private JMenuItem copyItem;
	private JMenuItem pasteItem;
	private JMenuItem selectAllItem;
	private JMenuItem cutItem;
	public TextComponentPopupMenu() {
		// canModify = textComp.isEditable();
		copyA = new CopyAction();
		pasteA = new PasteAction();
		selectAllA = new SelectAllAction();
		cutA = new CutAction();
		copyItem = new JMenuItem(copyA);
		pasteItem = new JMenuItem(pasteA);
		selectAllItem = new JMenuItem(selectAllA);
		cutItem = new JMenuItem(cutA);
		this.add(copyItem);
		this.add(pasteItem);
		this.add(selectAllItem);
		this.add(cutItem);
	}
	public void show(Component invoker, int x, int y) {
		super.show(invoker, x, y);
		textComp = (JTextComponent) invoker;
		setModifiable(textComp);
	}
	public void setModifiable(JTextComponent component) {
		canModify = textComp.isEditable();
		String sel = textComp.getSelectedText();
		if (canModify) {
			pasteItem.setEnabled(true);
			cutItem.setEnabled(true);
		} else {
			pasteItem.setEnabled(false);
			cutItem.setEnabled(false);
		}
		copyItem.setEnabled(sel != null && sel.length() > 0);
	}
	public class CopyAction extends AbstractAction {
		public CopyAction() {
			super("Copy");
			putValue(SHORT_DESCRIPTION, "Copy2");
		}
		public void actionPerformed(ActionEvent e) {
			textComp.copy();
			/*
			 * String selection = textComp.getSelectedText(); StringSelection
			 * data = new StringSelection(selection); Clipboard clipboard =
			 * Toolkit.getDefaultToolkit().getSystemClipboard();
			 * clipboard.setContents(data, data);
			 */
		}
	}
	public class PasteAction extends AbstractAction {
		public PasteAction() {
			super("Paste");
			putValue(SHORT_DESCRIPTION, "Paste2");
		}
		public void actionPerformed(ActionEvent e) {
			textComp.paste();
			/*
			 * Clipboard clipboard =
			 * Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable
			 * clipData = clipboard.getContents(clipboard); if (clipData !=
			 * null) { if
			 * (clipData.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			 * String s = ""; try { s = (String)
			 * (clipData.getTransferData(DataFlavor.stringFlavor));
			 * textComp.setText(s); } catch (UnsupportedFlavorException e1) {
			 * e1.printStackTrace(); } catch (IOException e1) {
			 * e1.printStackTrace(); } } }
			 */
		}
	}
	public class SelectAllAction extends AbstractAction {
		public SelectAllAction() {
			super("SelectAll");
			putValue(SHORT_DESCRIPTION, "SelectAll2");
		}
		public void actionPerformed(ActionEvent e) {
			SwingUtilities.invokeLater(new SelecterTextComponent((textComp)));
//			 textComp.selectAll();
		}
	}
	public class CutAction extends AbstractAction {
		public CutAction() {
			super("Cut");
			putValue(SHORT_DESCRIPTION, "Cut");
		}
		public void actionPerformed(ActionEvent e) {
			textComp.cut();
		}
	}
}
